<script setup></script>

<template>
  <!-- 登陆界面 -->
  <div class="body">
    <div class="login-wrap">
      <el-form class="login-container" :mode="form" ref="formRef">
        <h1 class="title">用户注册</h1>
        <el-form-item label="">
          <el-input type="text" v-model="form.username" placeholder="用户名" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-input type="text" v-model="form.account" placeholder="登录账号" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-input type="password" v-model="form.password" placeholder="登录密码" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="form.role" placeholder="请选择角色" style="width: 100%">
            <el-option label="教师" value="0"></el-option>
            <el-option label="学生" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="doSubmit()">提交</el-button>
        </el-form-item>
        <el-row style="text-align: center; margin-top: -10px">
          <!-- <el-col :span="12"><el-link type="primary">忘记密码</el-link></el-col> -->
          <el-col :span="24"><el-link type="primary" @click="gotoLogin()">用户登陆</el-link></el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: "Register",
  data() {
    return {
      form: {
        username: "",
        account: "",
        password: "",
        role: "",
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        role: [
          { required: true, message: '请选择角色', trigger: 'blur' },
        ]
      }
    }
  },
  methods: {
    doSubmit() {
      this.$request.post('/register', this.form).then(res => {
        if (res.code === 200) {
          this.$message.success("注册成功")
          this.$router.push("/")
        } else if (res.code == 502) {
          this.$message.error("账号已存在")
        } else {
          this.$message.error("注册失败")
        }
      }).catch(err => {
        this.$message.error("注册错误")
      })
    },
    gotoLogin() {
      this.$router.push("/")
    }
  }
}
</script>

<style scoped>
.body {
  width: 100%;
  height: 100%;
  /* 背景颜色 */
  background-image: linear-gradient(to bottom, #ffffff 0%, #9edaf6 100%);
}

.login-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 90vh;
}

.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
</style>
